package ynu.elm.Dao;
import tools.utils;
import ynu.elm.entities.LoadIn;
import ynu.elm.entities.User;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class LoadInDao extends LoadIn{
    public LoadInDao(User user){
        try {
            user.getResource().setCon(utils.getConnection());
            String sql="SELECT * FROM `loadin` WHERE `identity`=? AND `name`=? AND `password`=?";
            PreparedStatement preparedStatement = user.getResource().getCon().prepareStatement(sql);
            preparedStatement.setString(1,user.getInfo().getIdentity());
            preparedStatement.setString(2,user.getInfo().getName());
            preparedStatement.setString(3,user.getInfo().getPassword());
            user.getResource().setRs(preparedStatement.executeQuery());
            if(user.getResource().getRs().next()) user.setState(true);
            else {
                user.setState(false);
                System.out.println("无此用户！");
            }
            if(user.isState()){
                if(user.getInfo().getIdentity().equals("商家")){
                    new ClientUserDao(user);
                }else{
                    new AdminUserDao(user);
                }
            }
            else System.exit(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            utils.release(user.getResource().getRs(), user.getResource().getSt(), user.getResource().getCon());
        }
    }
}
